package com.cssy.xxkj.logic.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.byd.acegiext.domain.User;
import com.cssy.xxkj.logic.contants.BillTypeConstant;
import com.cssy.xxkj.logic.dao.LogicProductionCancelDAO;
import com.cssy.xxkj.logic.dao.LogicProductionCancelItemDAO;
import com.cssy.xxkj.logic.entity.LogicProductionDispatchingDetailCancelDO;
import com.cssy.xxkj.logic.entity.LogicProductionDispatchingDetailCancelItemDO;
import com.cssy.xxkj.logic.entity.LogicProductionDispatchingDetailDO;
import com.cssy.xxkj.logic.entity.LogicProductionDispatchingDetailItemDO;
import com.cssy.xxkj.logic.service.ILogicProductionCancelService;
import com.plat.exception.SyBaseException;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.GlobalResLoader;
import com.plat.util.IDCodeTypeUtil;
import com.plat.util.SessionUtil;
import com.yuendong.sy.po.code.SyCode;
import com.yuendong.sy.service.code.impl.SyCodeService;

/**
 * 生产派工取消
 * @author zeyong.gui
 *
 */
@Service("logicProductionCancelService")
public class LogicProductionCancelServiceImpl implements
		ILogicProductionCancelService {
	
	@Resource(name = "logicProductionCancelDAO")
	private LogicProductionCancelDAO logicProductionCancelDAO;
	
	@Resource(name = "logicProductionCancelItemDAO")
	private LogicProductionCancelItemDAO logicProductionCancelItemDAO;
	
	@Resource(name = "syCodeService")
	private SyCodeService syCodeService;
	
	@Override
	public SearchResult findListByExample(SearchConditions scs)
			throws SyBaseException {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public Map<String, BigDecimal> findQueryHasCancelToMap(Map<String, Object> paramMap)
			throws SyBaseException{
		List<LogicProductionDispatchingDetailCancelItemDO> cancelItemList = logicProductionCancelDAO.findListByMap(paramMap);
		if(cancelItemList==null||cancelItemList.isEmpty()||cancelItemList.size()==0){
			return null;
		}else{
			Map<String, BigDecimal> detailItemMap = new HashMap<String, BigDecimal>();
			for(LogicProductionDispatchingDetailCancelItemDO cancelItem : cancelItemList){
				if(cancelItem==null){
					continue;
				}
				String keyStr = paramMap.get("oriId")+"_"+cancelItem.getZd006MaterialsId()+"_"
					+cancelItem.getZd010MaterialsModel()+"_"
					+cancelItem.getZd017MaterialsSize()+"_"
					+cancelItem.getZd020MaterialsColor()+"_"
					+cancelItem.getZd021MaterialsQuality()+"_"
					+cancelItem.getZd023Process();
				BigDecimal zd013Quantity = cancelItem.getZd025CancelQuantity()==null?BigDecimal.ZERO:cancelItem.getZd025CancelQuantity();
				if(detailItemMap.get(keyStr)==null){
					detailItemMap.put(keyStr, zd013Quantity);
				}else{
					detailItemMap.put(keyStr, zd013Quantity.add(detailItemMap.get(keyStr)));
				}
			}
			return detailItemMap;
		} 
	}

	@Override
	public Integer delete(String id, User user) throws SyBaseException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer update(LogicProductionDispatchingDetailCancelDO t, User user)
			throws SyBaseException {
		t.setBizModifyName(user.getName());
		t.setBizModifyDate(new Date());
		t.setBizVersionMark(t.getBizVersion());
		t.setBizVersion(t.getBizVersion()+1);
		return logicProductionCancelDAO.updateLogicProductionDispatchingCancelDO(t);
	}

	@Override
	public LogicProductionDispatchingDetailCancelDO save(
			LogicProductionDispatchingDetailCancelDO cancel, User user)
			throws SyBaseException {
		cancel.setBizCreatorName(user.getName());
		cancel.setBizCreatorDate(new Date());
		cancel.setBizVersion(Integer.valueOf(1));
		logicProductionCancelDAO.insertLogicProductionDispatchingCancelDO(cancel);
		return cancel;
	}

	@Override
	public LogicProductionDispatchingDetailCancelDO cancelSave(LogicProductionDispatchingDetailDO detailEntity,
			HttpServletRequest request)throws SyBaseException{
		try{
			LogicProductionDispatchingDetailCancelDO cancel = new LogicProductionDispatchingDetailCancelDO();
			String cancelId = IDCodeTypeUtil.getId();
			String cancelCode = syCodeService.getCode(BillTypeConstant.PRODISPATCANCEL,BillTypeConstant.PRODISPATCANCEL,SyCode.DATE_YYMM,5);
			cancel.setId(cancelId);
			cancel.setZd001FormCode(cancelCode);
			cancel.setZd002OriId(detailEntity.getId());
			cancel.setZd002OriCode(detailEntity.getZd001FormCode());
			cancel.setZd003PiCode(detailEntity.getZd003PiCode());
			cancel.setZd004CustomerId(detailEntity.getZd004CustomerId());
			cancel.setZd004CustomerCode(detailEntity.getZd004CustomerCode());
			cancel.setZd004CustomerName(detailEntity.getZd004CustomerName());
			cancel.setZd005OrderDate(detailEntity.getZd005OrderDate());
			cancel.setZd006DeliveryDate(detailEntity.getZd006DeliveryDate());
			cancel.setZd007SalesMan(detailEntity.getZd007SalesMan());
			cancel.setZd009WorkShopCode(detailEntity.getZd011WorkShopCode());
			cancel.setZd010Remark(detailEntity.getZd009Remark());
			this.save(cancel, SessionUtil.getUser(request));
			cancel.setId(cancelId);
			return cancel;
		}catch(Exception e){
			e.printStackTrace();
			throw new SyBaseException(GlobalResLoader.getOpfailed()+ e.getMessage());
		}
	}
	
	@Override
	public void cancelItemSave(LogicProductionDispatchingDetailCancelDO cancelEntity,
			LogicProductionDispatchingDetailItemDO detailItem, BigDecimal theQuantity) throws SyBaseException{
		LogicProductionDispatchingDetailCancelItemDO cancelItem = new LogicProductionDispatchingDetailCancelItemDO();
		cancelItem.setId(IDCodeTypeUtil.getId());
		cancelItem.setZd001CancelId(cancelEntity.getId());
		cancelItem.setZd002CancelCode(cancelEntity.getZd001FormCode());
		
		cancelItem.setZd003CustomerId(detailItem.getZd003CustomerId());
		cancelItem.setZd004CustomerCode(detailItem.getZd004CustomerCode());
		cancelItem.setZd005CustomerName(detailItem.getZd005CustomerName());
		cancelItem.setZd006MaterialsId(detailItem.getZd006MaterialsId());
		cancelItem.setZd006MaterialsCode(detailItem.getZd006MaterialsCode());
		cancelItem.setZd006MaterialsName(detailItem.getZd006MaterialsName());
		cancelItem.setZd007UseCode(detailItem.getZd007UseCode());
		cancelItem.setZd008UseName(detailItem.getZd008UseName());
		cancelItem.setZd009MaterialsBatch(detailItem.getZd009MaterialsBatch());
		cancelItem.setZd010MaterialsModel(detailItem.getZd010MaterialsModel());
		cancelItem.setZd011MaterialsSpec(detailItem.getZd011MaterialsSpec());
		cancelItem.setZd012MaterialsUnit(detailItem.getZd012MaterialsUnit());
		cancelItem.setZd013Price(detailItem.getZd014Price());
		cancelItem.setZd014DeliveryDate(detailItem.getZd015DeliveryDate());
		cancelItem.setZd015MaterialsDescription(detailItem.getZd021MaterialsDescription());
		cancelItem.setZd016OpenBatch(detailItem.getZd022OpenBatch());
		cancelItem.setZd017MaterialsSize(detailItem.getZd023MaterialsSize());
		cancelItem.setZd018MaterialsPicture(detailItem.getZd024MaterialsPicture());
		cancelItem.setZd019MaterialsAnnex(detailItem.getZd025MaterialsAnnex());
		cancelItem.setZd020MaterialsColor(detailItem.getZd026MaterialsColor());
		cancelItem.setZd021MaterialsQuality(detailItem.getZd027MaterialsQuality());
		cancelItem.setZd022WorkShop(detailItem.getZd028WorkShop());
		cancelItem.setZd022WorkShopName(detailItem.getZd028WorkShopName());
		cancelItem.setZd023Process(detailItem.getZd029Process());
		cancelItem.setZd023ProcessName(detailItem.getZd029ProcessName());
		//订单数量
		cancelItem.setZd024PlanQuantity(detailItem.getZd030PlanQuantity());
		//派工数量
		cancelItem.setZd025CancelQuantity(theQuantity);
		this.logicProductionCancelItemDAO.insertLogicProductionCancelItemDO(cancelItem);
	}
	
	@Override
	public LogicProductionDispatchingDetailCancelDO findListById(String id)
			throws SyBaseException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer deleteBizRemoved(String id) throws SyBaseException {
		// TODO Auto-generated method stub
		return null;
	}

}
